package com.bj58.ecdata.dashboard.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bj58.ecdata.calc.dbservice.service.BusslineEnum;
import com.bj58.ecdata.dashboard.constant.EnumCate;
import com.bj58.ecdata.dashboard.databll.InfoServiceBLL;
import com.bj58.ecdata.dashboard.databll.PriviledgeBLL;
import com.bj58.ecdata.dashboard.service.SCFClient;
import com.bj58.ecdata.dashboard.utils.Option;
import com.bj58.ecdata.dashboard.utils.VelocityTools;
import com.bj58.groupbuy.action.interceptors.Check;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.util.SomePublicUtil;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.GET;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

@Path("/realtime")
public class InfoRealTimeController extends MvcController {
	private static final Log log = LogFactory.getLog(NetFlowRealTimeController.class);
	private final SomePublicUtil spu = new SomePublicUtil();
	private static final List<Option> busslineList = new ArrayList<Option>();
	private static final List<Option> platList = new ArrayList<Option>();
	private static final VelocityTools vt = new VelocityTools();

	{
		SCFClient.initialSCF();
		// 业务线选择
		busslineList.add(new Option("房产", "fangchan"));
		busslineList.add(new Option("黄页", "huangye"));
		busslineList.add(new Option("招聘", "zhaopin"));
		busslineList.add(new Option("二手", "ershou"));
		busslineList.add(new Option("二手车", "ershouche"));
		// 平台选择
		platList.add(new Option("PC", "PC"));
		platList.add(new Option("M", "M"));
		platList.add(new Option("APP", "APP"));
		platList.add(new Option("API", "API"));
		platList.add(new Option("帮帮", "帮帮"));
		platList.add(new Option("抓取", "抓取"));
		platList.add(new Option("其他", "其他"));
	}

	@Path("/info")
	@Check("dashboard/realtime/info")
	@POST
	@GET
	public ActionResult netFlowDashboard() {
		Map<String, Object> context = beat.getModel().getModel();
		try {
			HttpServletRequest request = beat.getRequest();
			User user = spu.getUserByCookie(request);
			// 检查业务线权限
			String[] businessOpers = PriviledgeBLL.businessOperCheck(user).split(",");
			if ("total".equals(businessOpers[0])) {
				businessOpers[0] = "";
			}
			String bussline_s = beat.getRequest().getParameter("bussline");
			String platform_s = beat.getRequest().getParameter("platform");

			BusslineEnum bussline = null;
			bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);

			context.put("bussline", bussline == null ? "" : bussline.value());
			context.put("platform", platform_s);
			context.put("busslineList", businessOpers);
			context.put("business_CN_Map", EnumCate.business_CN_Map);
			context.put("platList", platList);
			context.put("vt", vt);
			// pv vppv
			JSONObject cashcost = JSONObject.fromObject(InfoServiceBLL.getDailySum(platform_s, bussline));
			context.put("new", cashcost.get("value"));

			return ActionResult.view("realtime/info");
		} catch (Exception e) {
			log.info(e);
			return ViewResult.jsonView("");
		}
	}

	@POST
	@Path("/info/getNew")
	public ActionResult getNew() {
		String bussline_s = beat.getRequest().getParameter("bussline");
		String platform_s = beat.getRequest().getParameter("platform");

		BusslineEnum bussline = null;
		platform_s = StringUtils.isBlank(bussline_s) ? null : platform_s;
		try {
			bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
		} catch (Exception e) {
			log.info(e);
		}
		return ViewResult.jsonView(InfoServiceBLL.getDailySum(platform_s, bussline));
	}

	@POST
	@Path("/info/getHourData")
	public ActionResult getHourData() {
		String type_s = beat.getRequest().getParameter("type");
		String bussline_s = beat.getRequest().getParameter("bussline");
		String platform_s = beat.getRequest().getParameter("platform");
		String time_s = beat.getRequest().getParameter("time");
		try {
			BusslineEnum bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			String platform = StringUtils.isBlank(platform_s) ? null : platform_s;
			long time = NumberUtils.toLong(time_s, 0);

			return ViewResult.jsonpView(InfoServiceBLL.getData(type_s, platform, bussline, time));

		} catch (Exception e) {
		}

		return ViewResult.jsonView("{}");
	}

	@POST
	@Path("/info/getDayData")
	public ActionResult getDayData() {
		String type_s = beat.getRequest().getParameter("type");
		String bussline_s = beat.getRequest().getParameter("bussline");
		String platform_s = beat.getRequest().getParameter("platform");

		try {
			BusslineEnum bussline = StringUtils.isBlank(bussline_s) ? null : BusslineEnum.valueOf(bussline_s);
			String platform = StringUtils.isBlank(platform_s) ? null : platform_s;
			JSONObject yes = JSONObject.fromObject(InfoServiceBLL.getDataYesterdayHourly(type_s, platform, bussline));
			JSONObject now = JSONObject.fromObject(InfoServiceBLL.getDataTodayHourly(type_s, platform, bussline));
			now.element("yesdata", yes.get("data"));
			return ViewResult.jsonpView(now.toString());

		} catch (Exception e) {
		}
		return ViewResult.jsonView("{}");
	}

	@POST
	@Path("/tv/info/infocount")
	public ActionResult getInfoCount() {
		String num = beat.getRequest().getParameter("num");
		int top = NumberUtils.toInt(num, 5);

		try {

			return ViewResult.jsonView(InfoServiceBLL.getTopCateList(top));

		} catch (Exception e) {
			log.info(e);
		}
		return ViewResult.jsonView("{}");
	}

	@POST
	@Path("/tv/info/usercount")
	public ActionResult getInfoUserCount() {

		try {

			return ViewResult.jsonView(Long.toString(InfoServiceBLL.getCurrentUserCount()));

		} catch (Exception e) {
			log.info(e);
		}
		return ViewResult.jsonView("{}");
	}

}
